package net.zwj.service.impl;

import net.zwj.dao.mysql.MysqlDao;
import net.zwj.dao.oracle.OraclelDao;
import net.zwj.model.mysql.MysqlModel;
import net.zwj.model.oracle.OracleModel;
import net.zwj.service.SimpleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 
 * @author zhouwj
 *
 */

@Service
public class SimpleServiceImpl implements SimpleService {
	
	private MysqlDao mysqlDao;
	
	private OraclelDao oracleDao;
	
	@Autowired
	public void setOracleDao(OraclelDao oracleDao) {
		this.oracleDao = oracleDao;
	}

	@Autowired
	public void setMysqlDao(MysqlDao mysqlDao) {
		this.mysqlDao = mysqlDao;
	}
	
	@Override
	public void updateOracle() {
		OracleModel oracleModel = oracleDao.findOne(1L);
		System.out.println("cur: "+oracleModel.getNum());
		oracleModel.setNum(oracleModel.getNum()-1);
		oracleDao.save(oracleModel);
	}

	@Override
	public void updateMysql() {
		MysqlModel mysqlModel = mysqlDao.findOne(1L);
		System.out.println("cur: "+mysqlModel.getNum());
		mysqlModel.setNum(mysqlModel.getNum()-1);
		mysqlDao.save(mysqlModel);
	}

	@Override
	public void updateBoth() {
		OracleModel oracleModel = oracleDao.findOne(1L);
		oracleModel.setNum(oracleModel.getNum()-1);
		oracleDao.save(oracleModel);
		
		MysqlModel mysqlModel = mysqlDao.findOne(1L);
		mysqlModel.setNum(mysqlModel.getNum()+1);
		mysqlDao.save(mysqlModel);
		
	}

	@Override
	public void rollbackBoth() {
		OracleModel oracleModel = oracleDao.findOne(1L);
		MysqlModel mysqlModel = mysqlDao.findOne(1L);
		
		oracleModel.setNum(oracleModel.getNum()-1);
		if(oracleModel.getNum()>=mysqlModel.getNum()){
			throw new RuntimeException("roolback1");
		}
		
		mysqlModel.setNum(mysqlModel.getNum()+1);
		mysqlDao.save(mysqlModel);
		throw new RuntimeException("roolback2");
	}

	

}
